/*******************************************************************************
 * Copyright (c) 2010-2013 Federico Pecora <federico.pecora@oru.se>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/


import java.util.logging.Level;

import org.metacsp.framework.ConstraintNetwork;
import org.metacsp.framework.ValueOrderingH;
import org.metacsp.framework.VariableOrderingH;
import org.metacsp.meta.TCSP.MostConstrainedFirstVarOH;
import org.metacsp.meta.TCSP.TCSPLabeling;
import org.metacsp.meta.TCSP.TCSPSolver;
import org.metacsp.meta.TCSP.WidestIntervalFirstValOH;
import org.metacsp.multi.TCSP.DistanceConstraint;
import org.metacsp.multi.TCSP.DistanceConstraintSolver;
import org.metacsp.multi.TCSP.MultiTimePoint;
import org.metacsp.time.APSPSolver;
import org.metacsp.time.Bounds;
import org.metacsp.time.SimpleDistanceConstraint;
import org.metacsp.time.TimePoint;
import org.metacsp.utility.logging.MetaCSPLogging;


public class TestSTPSolverSimple {
	
	public static void main(String args[]) {
		
		APSPSolver groundSolver = new APSPSolver(0, 1000);		
		
		/*
		 * John travels to work by car (30-40 min).
		 * Fred goes to work in a carpool (40-50 min).
		 * Today John left home between 7:10 and 7:20 AM, and he arrived at work
		 * 10-20 min after Fred left home.
		 */
		
		TimePoint johnGoesToWork = (TimePoint)groundSolver.createVariable();
		TimePoint johnArrivesAtWork = (TimePoint)groundSolver.createVariable();
		TimePoint fredGoesToWork = (TimePoint)groundSolver.createVariable();
		TimePoint fredArrivesAtWork = (TimePoint)groundSolver.createVariable();
		
		ConstraintNetwork.draw(groundSolver.getConstraintNetwork(), "STP");
		
		SimpleDistanceConstraint johnTakesCar = new SimpleDistanceConstraint();
		johnTakesCar.setMinimum(30);
		johnTakesCar.setMaximum(40);
		johnTakesCar.setFrom(johnGoesToWork);
		johnTakesCar.setTo(johnArrivesAtWork);
		
		SimpleDistanceConstraint fredTakesCarpool = new SimpleDistanceConstraint();
		fredTakesCarpool.setMinimum(20);
		fredTakesCarpool.setMaximum(30);
		fredTakesCarpool.setFrom(fredGoesToWork);
		fredTakesCarpool.setTo(fredArrivesAtWork);
		
		SimpleDistanceConstraint johnLeaves = new SimpleDistanceConstraint();
		johnLeaves.setMinimum(10);
		johnLeaves.setMaximum(20);
		johnLeaves.setFrom(groundSolver.getSource());
		johnLeaves.setTo(johnGoesToWork);
		
		SimpleDistanceConstraint johnArrives = new SimpleDistanceConstraint();
		johnArrives.setMinimum(10);
		johnArrives.setMaximum(20);
		johnArrives.setFrom(johnArrivesAtWork);
		johnArrives.setTo(fredGoesToWork);
		
		groundSolver.addConstraints(johnTakesCar,fredTakesCarpool,johnLeaves,johnArrives);
		
		System.out.println("John goes to work at " + johnGoesToWork);
		System.out.println("John arrives at work at " + johnArrivesAtWork);
		System.out.println("Fred goes to work at " + fredGoesToWork);
		System.out.println("Fred arrives at work at " + fredArrivesAtWork);
		System.out.println("======= ^ Before sensing ^ ======");
		
		SimpleDistanceConstraint johnLeavesSensed = new SimpleDistanceConstraint();
		johnLeavesSensed.setMinimum(12);
		johnLeavesSensed.setMaximum(12);
		johnLeavesSensed.setFrom(groundSolver.getSource());
		johnLeavesSensed.setTo(johnGoesToWork);
		
		SimpleDistanceConstraint johnArrivesSensed = new SimpleDistanceConstraint();
		johnArrivesSensed.setMinimum(51);
		johnArrivesSensed.setMaximum(51);
		johnArrivesSensed.setFrom(groundSolver.getSource());
		johnArrivesSensed.setTo(johnArrivesAtWork);
		
		groundSolver.addConstraints(johnLeavesSensed,johnArrivesSensed);
		
		System.out.println("John goes to work at " + johnGoesToWork);
		System.out.println("John arrives at work at " + johnArrivesAtWork);
		System.out.println("Fred goes to work at " + fredGoesToWork);
		System.out.println("Fred arrives at work at " + fredArrivesAtWork);
		System.out.println("======= ^ After sensing ^ =======");
		
	}

}
